<?php
session_start();
//ini_set('display_errors',1);
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/database.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/class.admin.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/class.user.php');
$admin=new admin();


/*
Update Notes.
	1. Add administrators group
	2. Add the Admin user to Administrators group
	3. Insert all the admin pages and also loop through the current pages adding to the "admin_pages" collection.
This file runs once then renames itself to a date-stamped update_date.php for back tracking and roll backs
*/
$data=new database();
$data->db(DATABASE)->collection('users');
$data->criteria(array('username'=>1))->uniqueIndex();
$data->close();
$data=new database();
$data->db(DATABASE)->collection('groups');
$data->criteria(array('name'=>'Administrators'));
$row=$data->insert();
$gid=(string)$row['_id'];
$data->close();
$data=new database();
$data->db(DATABASE)->collection('groups');
$data->criteria(array('name'=>1))->uniqueIndex();
$data->close();
$data=new database();
$data->db(DATABASE)->collection('users');
$data->criteria(array('$set'=>array('groups'=>array($gid))));
$data->updateMultiple(array('username'=>'Admin'));
$data->close();

$listing=array(
'Delete page (action)'=>'/admin/resources/php/requests/actions/delete-content.php',
'Add page (action)'=>'/admin/resources/php/requests/actions/add-content.php',
'Save site settings (action)'=>'/admin/resources/php/requests/actions/save-site-settings.php',
'Create a form (action)'=>'/admin/resources/php/requests/actions/add-new-form.php',
'Delete media (action)'=>'/admin/resources/php/requests/actions/delete-image.php',
'Upload a theme (action)'=>'/admin/resources/php/requests/actions/upload_theme.php',
'Save page (action)'=>'/admin/resources/php/requests/actions/save-page.php',
'Save modules (action)'=>'/admin/resources/php/requests/actions/save-modules.php',
'Save group (action)'=>'/admin/resources/php/requests/actions/save-group.php',
'Delete page (action)'=>'/admin/resources/php/requests/actions/delete-page.php',
'Upload media (action)'=>'/admin/resources/php/requests/actions/upload_media.php',
'Delete module (action)'=>'/admin/resources/php/requests/actions/delete-module.php',
'Delete group (action)'=>'/admin/resources/php/requests/actions/delete-group.php',
'Upload logo (action)'=>'/admin/resources/php/requests/actions/upload_logo.php',
'Add new page (action)'=>'/admin/resources/php/requests/actions/add-page.php',
'Save form (action)'=>'/admin/resources/php/requests/actions/save-form.php',
'Upload Module (action)'=>'/admin/resources/php/requests/actions/upload_module.php',
'Save blog post (action)'=>'/admin/resources/php/requests/actions/save-blog-post.php',
'Add new blog post (action)'=>'/admin/resources/php/requests/actions/add-blog-post.php',
'Add group (action)'=>'/admin/resources/php/requests/actions/add-group.php',
'Show page content (list)'=>'/admin/resources/php/requests/lists/page-content.php',
'Show forms (list)'=>'/admin/resources/php/requests/lists/forms.php',
'Show pages (list)'=>'/admin/resources/php/requests/lists/pages.php',
'Show current pages (list)'=>'/admin/resources/php/requests/lists/current-pages.php',
'Show media (list)'=>'/admin/resources/php/requests/lists/media.php',
'Show blog posts / add blog post (form)'=>'/admin/resources/php/requests/forms/blog-posts.php',
'Add new group (form) / Show groups'=>'/admin/resources/php/requests/forms/add-group.php',
'Edit form (form)'=>'/admin/resources/php/requests/forms/edit-form.php',
'Edit blog post (form)'=>'/admin/resources/php/requests/forms/edit-blog-post.php',
'Edit group (form)'=>'/admin/resources/php/requests/forms/edit-group.php',
'Add new form / show form list (form)'=>'/admin/resources/php/requests/forms/forms.php',
'Show site settings (form)'=>'/admin/resources/php/requests/forms/site-settings.php',
'Add new page (form)'=>'/admin/resources/php/requests/forms/add-page.php',
'Add module (form)'=>'/admin/resources/php/requests/forms/modules.php',
'Add image (form)'=>'/admin/resources/php/requests/forms/add_image.php',
'Edit page (form)'=>'/admin/resources/php/requests/forms/edit-page.php',
'Index page'=>'/admin/index.php',
'Updater'=>'/admin/update.php'
);
$data=new database();
$data->db(DATABASE)->collection('admin_pages');
	foreach($listing as $key=>$val) {
	$data->criteria(array('$addToSet' => array("groups" => $gid)));
	$data->updateMultiple(array('path'=>$val,'role'=>'functionality','name'=>$key));

	}
$data->close();
$data=new database();
$data->db(DATABASE)->collection('pages');
$data->criteria(array('url'=>array('$exists'=>true)))->sort(array('url'=>1));
$rows=$data->findNl();
$data->close();
$data=new database();
$data->db(DATABASE)->collection('admin_pages');
	foreach($rows['results'] as $row) {
	$data->criteria(array('$addToSet' => array("groups" => $gid)));
	$data->updateMultiple(array('path'=>$row['url'],'role'=>'page','name'=>$row['text']));
	}
$data->close();
// make a bundle now ....
$admin->createAdminBundle();
rename($_SERVER['DOCUMENT_ROOT'].'/admin/update.php',$_SERVER['DOCUMENT_ROOT'].'/admin/update_'.date("d_m_y_g_i_s",time()).'.php');
$_SESSION['logged_in']=false;
?>
<script>
if(confirm('Site has been updated - we must now reload the cache')) {
document.location.href='/admin/';
}
</script>